define(function (require) {
    require('bootstrapValidator');
    require('messager');
    require('table');
    require('typeahead');
    require('doctor-data');
    require('fileinput');
    require('daterangepicker');
    require('doctor-select');
    require('moment');
    require('clockpicker');
    require('datetimepicker');
    require('iCheck');

    $.module("BASE.BDMSaleLines", function () {

        return {
            init: function () {
                this.loadData();
                this.loadEvent();
            },
            clearDatas:function () {
                var that = this;

                $("#_id").val(0);

                $('#fm').bootstrapValidator('resetForm', true);
                that.showDiv('main_div');
            },
            loadData: function (pageNumber, pageSize) {
                var that = this;

                let s_id = $("#s_id").val();
                let s_BillCode = $("#s_BillCode").val();
                let s_CompanyCode = $("#s_CompanyCode").val();
                let s_CompanyName = $("#s_CompanyName").val();
                let s_CustomerName = $("#s_CustomerName").val();
                let s_CustomerCode = $("#s_CustomerCode").val();
                let s_SalesCostCenterName = $("#s_SalesCostCenterName").val();
                let s_MaterialCode = $("#s_MaterialCode").val();
                let s_MaterialName = $("#s_MaterialName").val();

                SYS.Core.ajax({
                    url: "base/saleLines/list",
                    common: {
                        page: pageNumber ? pageNumber : 1,
                        size: pageSize ? pageSize : 20
                    },
                    data: {
                        BillCode: s_BillCode,
                        CompanyCode: s_CompanyCode,
                        CompanyName: s_CompanyName,
                        CustomerCode: s_CustomerCode,
                        CustomerName: s_CustomerName,
                        SalesCostCenterName: s_SalesCostCenterName,
                        MaterialCode: s_MaterialCode,
                        MaterialName: s_MaterialName,
                    },
                    success: function (data) {
                        var obj = {
                            'pageNumber': data.data.extra.current_page,
                            'pageSize': data.data.extra.page_size,
                            'totalRows': data.data.extra.all_count,
                            'data': data.data.list
                        };

                        $("#myTable").bootstrapTable('destroy');
                        $('#myTable').bootstrapTable({
                            striped: true,
                            singleSelect: false,
                            showColumns: true,
                            showToggle: true,
                            pagination: true,
                            pageNumber: obj.pageNumber,
                            pageSize: obj.pageSize,
                            totalRows: obj.totalRows,
                            pageList: [20, 50, 100, 200],
                            sidePagination: 'server',
                            clickToSelect: true,
                            idField: 'id',
                            data: obj.data,
                            columns: [
                                // {field: 'checked', checkbox: true} ,
                                {field: 'Id', title: '编号', align: 'center'},
                                {field: 'BillId', title: '应收单ID', align: 'center'},
                                {field: 'BillCode', title: '应收单编号', align: 'center'},
                                {field: 'BillItemId', title: '应收单明细ID', align: 'center'},

                                {field: 'CompanyId', title: '法人公司ID', align: 'center'},
                                {field: 'CompanyName', title: '法人公司名称', align: 'center'},
                                {field: 'CompanyCode', title: '法人公司编码', align: 'center'},

                                {field: 'CustomerId', title: '客户ID', align: 'center'},
                                {field: 'CustomerName', title: '客户名称', align: 'center'},
                                {field: 'CustomerCode', title: '客户编码', align: 'center'},

                                {field: 'SalesCostCenterId', title: '客户的销售组ID', align: 'center'},
                                {field: 'SalesCostCenterName', title: '客户的销售组名称', align: 'center'},

                                {field: 'PeriodId', title: '会计期间Id', align: 'center'},
                                {field: 'PeriodYear', title: '会计期间年', align: 'center'},
                                {field: 'PeriodQuarter', title: '会计期间季', align: 'center'},
                                {field: 'PeriodMonth', title: '会计期间月', align: 'center'},
                                {field: 'PeriodCode', title: '会计期间代码', align: 'center'},

                                {field: 'BizDate', title: '业务日期', align: 'center'},

                                {field: 'MaterialId', title: '物料ID', align: 'center'},
                                {field: 'MaterialName', title: '物料名称', align: 'center'},
                                {field: 'MaterialCode', title: '物料编码', align: 'center'},
                                {field: 'Quantity', title: '数量', align: 'center'},
                                {field: 'PackQuantity', title: '数量（箱）', align: 'center'},
                                {field: 'StdPackQuantity', title: '数量（标准）', align: 'center'},
                                {field: 'SalesPrice', title: '销售单价', align: 'center'},
                                {field: 'SalesAmount', title: '金额', align: 'center'},

                                {field: 'RevenueExternalAmount', title: '收入', align: 'center'},
                                {field: 'RevenueInternalAmount', title: '收入', align: 'center'},
                                {field: 'RevenueAccountViewCode', title: '收入对应的科目代码（法人公司科目）', align: 'center'},
                                {field: 'RevenueAccountViewName', title: '收入对应的科目名称（法人公司科目）', align: 'center'},
                                {field: 'RevenueGroupAccountViewCode', title: '收入对应的科目（集团科目）', align: 'center'},

                                {field: 'CostExternalAmount', title: '成本', align: 'center'},
                                {field: 'CostInternalAmount', title: '成本', align: 'center'},
                                {field: 'CostAccountViewCode', title: '成本科目Code（法人公司科目）', align: 'center'},
                                {field: 'CostAccountViewName', title: '成本科目Name（法人公司科目）', align: 'center'},
                                {field: 'CostGroupAccountViewCode', title: '成本对应的科目（集团科目）', align: 'center'},

                                {field: 'DataSource', title: '数据来源', align: 'center',
                                    formatter:function (val,row) {
                                        let html = '';
                                        if (val == 10){
                                            html = '应收单明细'
                                        } else if (val == 20){
                                            html = '销售出库单'
                                        } else if (val == 30){
                                            html = '总账凭证虚拟'
                                        }
                                        return html;
                                    }
                                },
                                {field: 'JournalEntryId', title: '总账分录Id', align: 'center'},
                                {field: 'Etl_Last_Date', title: 'ETL时间', align: 'center'},
                                // {field: 'PeriodDate', title: '本月1号', align: 'center'},
                                {field: 'Source', title: '来源', align: 'center',
                                    formatter:function (val,row) {
                                        let html = '';
                                        if (val == 10){
                                            html = '系统'
                                        } else if (val == 20){
                                            html = '手动'
                                        }
                                        return html;
                                    }
                                },

                                // {
                                //     field: 'do', title: '操作', align: 'center',
                                //     formatter: function (value, row, index) {
                                //         var html = '';
                                //         html += '<a href="javascript:void(0)" onclick="BASE.BDMSaleLines.toEdit(' + row.id + ')" class="btn btn-edit">编辑</a> | ';
                                //         html += '<a href="javascript:void(0)" onclick="BASE.BDMSaleLines.toRemove(' + row.id + ')" class="btn btn-remove  ">删除</a>';
                                //         return html;
                                //     }
                                // }
                            ],
                            onPageChange: function(number, size){
                                that.loadData(number, size);
                            },
                            onRefreshTable: function () {
                                that.loadData();
                            },
                            onClickCell: function (field, value, row, $element) {
                            }
                        });
                    }
                });
            },
            loadEvent: function () {
                var that = this;

                // 定义编辑框form
                var $form = $("#fm");

                // 查询
                $("#search-toolbar").on('click', "#btn_search", function () {
                    that.loadData();
                }).on("keydown ", "input", function (e) {
                    if (e.keyCode == 13) {
                        that.loadData();
                    }
                });

                //批量删除
                $("#btn_deletes").on('click',function(){
                    that.toRemoves();
                });

                // 保存新增
                $("#btn_contiune_add").on('click', function () {
                    // 不用关闭当前编辑框
                    that.save(1);
                });

                // 新增
                $("#btn_add").on('click', function () {
                    that.clearDatas();

                    $("#btn-add-div").show();
                    $("#btn-edit-div").hide();
                    that.showDiv('edit_div');
                });

                // 返回
                $(".back_list").on('click', function () {
                    that.clearDatas();

                    that.showDiv('main_div');
                });

                //
                $('#fm').bootstrapValidator({
                    excluded: ':disabled',
                    feedbackIcons: {
                        valid: 'glyphicon glyphicon-ok',
                        invalid: 'glyphicon glyphicon-remove',
                        validating: 'glyphicon glyphicon-refresh'
                    },
                    fields: {
                        // title: {
                        //     validators: {
                        //         notEmpty: {
                        //             message: '此项必填'
                        //         }
                        //     }
                        // },
                        // order: {
                        //     validators: {
                        //         notEmpty: {
                        //             message: '此项必填'
                        //         },
                        //         regexp: {
                        //             regexp: /^[1-9]\d*$/i,
                        //             message: '格式不正确，必须大于0'
                        //         }
                        //     }
                        // }
                    }
                }).on('success.form.bv', function (e) {
                    e.preventDefault();

                    that.save(0);
                });

            },
            save: function () {
                var that = this;

                var id = $("#_id").val();
                var cardname = $("#cardname").val();
                var num = $("#num").val();
                var lnum = $("#lnum").val();

                SYS.Core.ajax({
                    url: 'base/card/save',
                    common: {},
                    data:{
                        id: id || 0,
                        cardname: cardname,
                        num: num,
                        lnum: lnum
                    },
                    success: function (data) {
                        if(data.code == 1) {
                            $.messager.popup(data.msg, 'success');

                            if (isContinus) {
                                that.clearDatas();
                                that.loadData();
                                that.showDiv('edit_div');
                            } else {
                                that.loadData();
                                that.showDiv('main_div');
                            }

                        } else {
                            $('#fm').bootstrapValidator('disableSubmitButtons', false);
                            $.messager.popup(data.msg, 'error');
                        }
                    }
                })
            },
            toEdit: function (id) {
                var that = this;
                that.clearDatas();
                $("#btn-add-div").hide();
                $("#btn-edit-div").show();

                SYS.Core.ajax({
                    url: 'base/saleLines/detail',
                    common: {},
                    data: {
                        id: id ? id : 0
                    },
                    success: function (data) {
                        if (data.code == 1) {
                            var obj = data.data;
                            let id = obj.id;
                            let cardname = obj.cardname;
                            let num = obj.num;
                            let lnum = obj.lnum;

                            $("#_id").val(id);
                            $("#cardname").val(cardname);
                            $("#num").val(num);
                            $("#lnum").val(lnum);

                            that.showDiv('edit_div');
                        }
                    }
                });
            },
            showDiv: function (div) {
                let divs = ['main_div','edit_div','import_div'];
                for(var i=0; i<divs.length; i++){
                    $("#" + divs[i]).hide();
                }
                $("#" + div).show();
            }
        }
    });
    BASE.BDMSaleLines.init();
});